package com.dsteiner.gwt.cafeteriasophia.client;

import java.beans.PropertyChangeListener;

import com.dsteiner.gwt.cafeteriasophia.client.controller.ChangeController;
import com.dsteiner.gwt.cafeteriasophia.client.domain.Change;
import com.dsteiner.gwt.cafeteriasophia.client.model.ProductModel;
import com.dsteiner.widgets.client.PrefilledTextBox;
import com.dsteiner.widgets.client.PropertyChangeCollection;
import com.dsteiner.widgets.client.Widgets.ButtonExt;
import com.dsteiner.widgets.client.Widgets.HTMLExt;
import com.dsteiner.widgets.client.Widgets.HorizontalPanelExt;
import com.dsteiner.widgets.client.Widgets.LazyWidget;
import com.dsteiner.widgets.client.event.EventManagerImpl;
import com.dsteiner.widgets.client.model.PrefilledTextBoxModel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Widget;

public class ChangeWidget extends LazyWidget{

	public static class ChangeModel {
		private PropertyChangeCollection propertyChangeSupport = new PropertyChangeCollection(
				this);

		public void addPropertyChangeListener(String propertyName,
				PropertyChangeListener listener) {
			propertyChangeSupport.registerPropertyChangeListener(propertyName,
					listener);
		}
		private Change change;

		public Change getChange() {
			return change;
		}

		public void setChange(Change change) {
			Change oldChange = this.change;
			this.change = change;
			propertyChangeSupport.firePropertyChange("change", oldChange, change);
		}
		
		private ProductModel productModel;

		public ProductModel getProductModel() {
			return productModel;
		}

		public void setProductModel(ProductModel productModel) {
			ProductModel oldProductModel = this.productModel;
			this.productModel = productModel;
			propertyChangeSupport.firePropertyChange("productModel", oldProductModel, productModel);
		}
		
		private Change moneyChange;

		public Change getMoneyChange() {
			return moneyChange;
		}

		public void setMoneyChange(Change moneyChange) {
			Change oldMoneyChange = this.moneyChange;
			this.moneyChange = moneyChange;
			propertyChangeSupport.firePropertyChange("moneyChange", oldMoneyChange, moneyChange);
		}
	}
	
	private ChangeController changeController = new ChangeController();
	private final ChangeModel changeModel;

	public ChangeWidget(ChangeModel changeModel) {
		this.changeModel = changeModel;
	}

	@Override
	public Widget createContentWidget() {
		FlowPanel mainPanel = new FlowPanel();
		HTMLExt infoHtml = new HTMLExt();
		ButtonExt addSalesButton = new ButtonExt("");
		ButtonExt addPurchasesButton = new ButtonExt("");
		PrefilledTextBoxModel stockTextBoxModel = new PrefilledTextBoxModel();
		PrefilledTextBox stockTextBox = new PrefilledTextBox(stockTextBoxModel);
		PrefilledTextBoxModel addSalesTextBoxModel = new PrefilledTextBoxModel();
		PrefilledTextBox addSalesTextBox = new PrefilledTextBox(addSalesTextBoxModel);
		PrefilledTextBoxModel addPurchasesTextBoxModel = new PrefilledTextBoxModel();
		PrefilledTextBox addPurchasesTextBox = new PrefilledTextBox(addPurchasesTextBoxModel);
		stockTextBoxModel.setDefaultText("recontar el inventario y ingresarlo aqui");
		HorizontalPanelExt addSalesPanel = new HorizontalPanelExt();
		HorizontalPanelExt addPurchasesPanel = new HorizontalPanelExt();
		HorizontalPanel horizontalPanel = new HorizontalPanel();
		HorizontalPanelExt addPurchasePricePanel = new HorizontalPanelExt();
		horizontalPanel.setSpacing(5);
		addSalesPanel.setSpacing(5);
		addPurchasesPanel.setSpacing(5);
		addPurchasePricePanel.setSpacing(5);
		
		mainPanel.add(infoHtml);
		mainPanel.add(horizontalPanel);
		
		horizontalPanel.add(stockTextBox);
		horizontalPanel.add(addSalesPanel);
		horizontalPanel.add(addPurchasesPanel);
		
		addSalesPanel.add((Widget)addSalesButton);
		addSalesPanel.add(new HTML("a un precio de"));
		addSalesPanel.add((Widget)addSalesTextBox);
		
		addPurchasesPanel.add((Widget)addPurchasesButton);
		addPurchasesPanel.add((Widget)addPurchasePricePanel);
		addPurchasePricePanel.add(new HTML("a un precio de"));
		addPurchasePricePanel.add((Widget)addPurchasesTextBox);

		stockTextBox.setWidth("230px");
		addSalesTextBox.setWidth("50px;");
		changeController.registerInfoHtml(infoHtml);
		changeController.registerAddSalesButton(addSalesButton);
		changeController.registerAddSalesPanel(addSalesPanel);
		changeController.registerAddSalesTextBoxModel(addSalesTextBoxModel);
		changeController.registerAddPurchasesButton(addPurchasesButton);
		changeController.registerAddPurchasesPanel(addPurchasesPanel);
		changeController.registerAddPurchasesPricePanel(addPurchasePricePanel);
		changeController.registerAddPurchasesTextBoxModel(addPurchasesTextBoxModel);
		changeController.registerChangeWidget(this);
		changeController.registerChangeModel(changeModel);
		changeController.registerStockTextBoxModel(stockTextBoxModel);
		return mainPanel;
	}

}
